package com.cloud.springbootcloud.service.impl;

import com.cloud.springbootcloud.pojo.Order;
import com.cloud.springbootcloud.pojo.Product;
import com.cloud.springbootcloud.service.OrderService;
import com.cloud.springbootcloud.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired(required=false)
    private ProductService productService;
    /**
     * 根据主键查询订单
     *
     * @param id
     * @return
     */
    public Order selectOrderById(Integer id) {
        return new Order(id, "1001", "湖北", 300D, productService.getProductList());
    }

    /**
     * 按发现客户端选择产品列表
     * @return
     */
    private List<Product> selectProductListByDiscoveryClient() {
        StringBuffer stringBuffer;
        //获取服务列表
        List<String> serverIds = discoveryClient.getServices();
        if (CollectionUtils.isEmpty(serverIds)) return null;
        // 根据服务名称获取服务
        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("service-provider");
        if (CollectionUtils.isEmpty(serviceInstances)) return null;
        ServiceInstance serviceInstance = serviceInstances.get(0);
        stringBuffer = new StringBuffer();
        stringBuffer.append("http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/product/list");
        System.out.println(stringBuffer.toString());
        // ResponseEntity: 封装了返回响应实体数据
        ResponseEntity<List<Product>> response = restTemplate.exchange(
                stringBuffer.toString(),
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<Product>>() {}
        );
        return response.getBody();
    }

    /**
     * 按负载均衡器客户端选择产品列表
     * @return
     */

    private List<Product> selectProductListByLoadBalancerClient() {
        StringBuilder stringBuilder;
        // 根据服务名称获取服务
        ServiceInstance serviceInstance = loadBalancerClient.choose("service-provider");
        if (null == serviceInstance){
            return null;
        }
        stringBuilder = new StringBuilder();
        stringBuilder.append("http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/product/list");
        System.out.println(stringBuilder.toString());
        // ResponseEntity: 封装了返回数据
        ResponseEntity<List<Product>> response =
                restTemplate.exchange(stringBuilder.toString(), HttpMethod.GET, null,
                        new ParameterizedTypeReference<List<Product>>() {
                        });
        return response.getBody();
    }
    /**
     * 按负载均衡器注释选择产品列表
     * @return
     */
    private List<Product> selectProductListByLoadBalancerAnnotation() {
        // ResponseEntity: 封装了返回响应实体数据
        ResponseEntity<List<Product>> response = restTemplate.exchange(
                "http://service-provider/product/list",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<Product>>() {}
        );
        return response.getBody();
    }

}
